package com.whnk.pm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whnk.common.pm.enums.PmEmpSignType;
import com.whnk.common.utils.StringUtils;
import com.whnk.pm.domain.EmpSignInfo;
import com.whnk.pm.domain.EventData;
import com.whnk.pm.domain.FullcalendarData;
import com.whnk.pm.mapper.EmpSignInfoMapper;
import com.whnk.pm.service.IEmpSignInfoService;

/**
 * 个人考勤 服务层实现
 * 
 * @author whwhnk
 * @date 2019-09-27
 */
@Service
public class EmpSignInfoServiceImpl implements IEmpSignInfoService {
	
	@Autowired
	private EmpSignInfoMapper EmpSignInfoMapper;

	/**
     * 根据年月和身份证，查询个人考勤列表，并组装成Fullcalendar日历
     * 
     * @param empSignInfo 
     * @return 
     */
	@Override
	public FullcalendarData selectEmpSignInfos(EmpSignInfo empSignInfo){
		List<EmpSignInfo> list = EmpSignInfoMapper.selectEmpSignInfoList(empSignInfo);
		
		FullcalendarData data= new FullcalendarData();
		List<EventData> events = new ArrayList<EventData>();
		
		for(EmpSignInfo item : list) {
			if(!StringUtils.isEmpty(item.getSignNote())) {
				addEvent(events,StringUtils.isEmpty(item.getSignTime())?"------":item.getSignTime(),item.getWdDate());
				addEvent(events,item.getSignNote(),item.getWdDate());
			}
		}
		data.setEvents(events);
		
		return data;
	}
	
	/**
	 * 添加事件
	 * 
	 * @param events
	 * @param title
	 * @param date
	 */
	private void addEvent(List<EventData> events,String title,String date) {
		EventData event = new EventData();
		event.setTitle(title.indexOf("-")>-1?title:PmEmpSignType.getValueByKey(title));
		event.setStart(date);
		event.setEditable(false);
		event.setUrl("#");//如果不设置成#，则页面上单击事件会跳转到空页面。
		
		// 缺勤，红色
		if(PmEmpSignType.WORK_ABSENCE.getCode().equals(title) ) 
			event.setBackgroundColor("red");
		
		// 请假，绿色
		else if(PmEmpSignType.WORK_LEAVE.getCode().equals(title) ) 
			event.setBackgroundColor("green");
		
		
		events.add(event);
	}
}
